﻿using Contracts;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entities.CommandPersist
{
    public class EFCommandRepository : ICommandStore
    {
        void ICommandStore.Create<TEntity>(TEntity entity)
        {
            db.Set<TEntity>().Add(entity);
            db.SaveChanges();
        }

        void ICommandStore.Delete<TEntity>(TEntity entity)
        {
            db.Set<TEntity>().Remove(entity);
            db.SaveChanges();
        }

        void ICommandStore.Update<TEntity>(TEntity entity)
        {
            db.Entry<TEntity>(entity).State = EntityState.Modified;
            db.SaveChanges();
        }

        private CommandDbContext db = new CommandDbContext();

        #region Implement IDisposable
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    db.Dispose();
                }

                disposedValue = true;
            }
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
        }
        #endregion
    }
}
